package com.hnzq.cloud.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hnzq.cloud.api.FreGoodsAPI;
import com.hnzq.cloud.dto.FreGoodsDto;
import com.hnzq.cloud.entity.FreFlag;
import com.hnzq.cloud.entity.FreGoods;
import com.hnzq.cloud.entity.FreGoodsFlag;
import com.hnzq.cloud.entity.FreType;
import com.hnzq.cloud.exception.FreFlagException;
import com.hnzq.cloud.exception.FreTypeException;
import com.hnzq.cloud.model.Result;
import com.hnzq.cloud.param.AddFreGoodsInfoParam;
import com.hnzq.cloud.param.UpdateFreGoodsInfoParam;
import com.hnzq.cloud.param.UpdateStockParam;
import com.hnzq.cloud.service.FreFlagService;
import com.hnzq.cloud.service.FreGoodsFlagService;
import com.hnzq.cloud.service.FreGoodsService;
import com.hnzq.cloud.service.FreTypeService;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jams
 * @since 2022-02-06
 */
@RestController
public class FreGoodsController implements FreGoodsAPI {

    @Resource
    private FreGoodsService freGoodsService;

    @Resource
    private FreTypeService freTypeService;

    @Resource
    private FreFlagService freFlagService;

    @Resource
    private FreGoodsFlagService freGoodsFlagService;

    /**
    * @作者: 倪西
    * @描述：新增商品信息
    * @时间：2022/2/6 16:38
    * @参数：[addFreGoodsInfoParam]
    * @返回：com.woniuxy.result.Result
    **/
    public Result addFreGoods(AddFreGoodsInfoParam addFreGoodsInfoParam){
        Boolean bool = freGoodsService.addFreGoods(addFreGoodsInfoParam);
        if (bool){
            return Result.newInstance().setMessage("添加成功");
        }else {
            return Result.newInstance().setMessage("添加失败").setCode(144);
        }
    }

    /**
    * @作者: 倪西
    * @描述：对应商品的id进行上架操作
    * @时间：2022/2/6 16:48
    * @参数：[freGoodsId]
    * @返回：com.woniuxy.result.Result
    **/
    public Result putOnFreGoods(Integer freGoodsId){
        Boolean bool = freGoodsService.putOnFreGoods(freGoodsId);
        if (bool){
            return Result.newInstance().setMessage("上架成功");
        }else {
            return Result.newInstance().setMessage("上架失败").setCode(144);
        }
    }
    /**
    * @作者: 倪西
    * @描述：对应商品的id进行下架操作
    * @时间：2022/2/6 16:50
    * @参数：[freGoodsId]
    * @返回：com.woniuxy.result.Result
    **/
    public Result offFreGoods(Integer freGoodsId){
        Boolean bool = freGoodsService.offFreGoods(freGoodsId);
        if (bool){
            return Result.newInstance().setMessage("下架成功");
        }else {
            return Result.newInstance().setMessage("下架失败").setCode(144);
        }
    }

    /**
    * @作者: 倪西
    * @描述：对应商品的基本信息修改更新
    * @时间：2022/2/11 10:14
    * @参数：[updateFreGoodsInfoParam]
    * @返回：com.hnzq.cloud.model.Result
    **/
    public Result changeFreGoodsInfo(UpdateFreGoodsInfoParam updateFreGoodsInfoParam){
        Boolean bool = freGoodsService.changeFreGoodsInfo(updateFreGoodsInfoParam);
        if (bool){
            return Result.newInstance().setMessage("修改成功");
        }else {
            return Result.newInstance().setMessage("修改失败").setCode(144);
        }
    }

    /**
    * @作者: 倪西
    * @描述：对商品id对应的商品进行逻辑删除
    * @时间：2022/2/11 10:20
    * @参数：[freGoodsId]
    * @返回：com.hnzq.cloud.model.Result
    **/
    public Result deleteFreGoods(List<Integer> ids){
        Boolean bool = freGoodsService.deleteFreGoods(ids);
        if (bool){
            return Result.newInstance().setMessage("删除成功");
        }else {
            return Result.newInstance().setMessage("删除失败").setCode(144);
        }
    }
    /**
    * @作者: 倪西
    * @描述：对库存进行操作
    * @时间：2022/2/11 11:51
    * @参数：[updateStockParams]
    * @返回：java.lang.Boolean
    **/
    public Result updateFreGoodsStock(List<UpdateStockParam> updateStockParams){
        Boolean bool = freGoodsService.updateFreGoodsStock(updateStockParams);
        if (bool){
            return Result.newInstance().setMessage("库存修改成功");
        }else {
            return Result.newInstance().setMessage("库存修改失败").setCode(144);
        }
    }

    /**
    * @作者: 倪西
    * @描述：分页查询所有的商品
    * @时间：2022/2/11 17:08
    * @参数：[]
    * @返回：com.hnzq.cloud.model.Result
    **/
    public Result findAllGoods(){
        Page<FreGoods> freGoods = freGoodsService.findAll();
        return Result.newInstance().setMessage("查询成功").setData(freGoods);
    }

    @Override
    public Result getGoodsByType(Integer typeId) {
        FreType freType = freTypeService.getById(typeId);
        if (null==freType) throw new FreTypeException("该类型不存在",244);
        QueryWrapper<FreGoods> wrapper = new QueryWrapper<>();
        wrapper.eq("type_name",freType.getTypeName());
        List<FreGoods> goodsList = freGoodsService.list(wrapper);
        return Result.newInstance().setMessage("查询成功").setData(goodsList);
    }

    @Override
    public Result getGoodsByFlag(Integer flagId) {
        FreFlag flag = freFlagService.getById(flagId);
        if (null==flag) throw new FreFlagException("标签不存在",344);
        QueryWrapper<FreGoodsFlag> wrapper = new QueryWrapper<>();
        wrapper.eq("flag_id",flagId);
        List<FreGoodsFlag> goodsFlagList = freGoodsFlagService.list(wrapper);
        List<Integer> goodsIds = goodsFlagList.stream()
                .map(item -> item.getGoodsId()).collect(Collectors.toList());
        List<FreGoods> freGoods = freGoodsService.listByIds(goodsIds);
        return Result.newInstance().setMessage("查询成功").setData(freGoods);
    }

    /**
    * @作者: 倪西
    * @描述：根据关键词对商品进行搜索查询
    * @时间：2022/2/21 15:36
    * @参数：[keyword]
    * @返回：com.hnzq.cloud.model.Result
    **/
    public Result selectGoodsByKeyword(String keyword){
        Page<FreGoods> freGoods = freGoodsService.selectGoodsByKeyword(keyword);
        return Result.newInstance().setMessage("查询成功").setData(freGoods);
    }

    /**
    * @作者: 倪西
    * @描述：通过ids查询对应商品的详细信息
    * @时间：2022/2/12 11:36
    * @参数：[id]
    * @返回：com.hnzq.cloud.model.Result
    **/
    public Result<Map<Integer, FreGoodsDto>> findGoodsInfos(List<Integer> ids){
        Map<Integer, FreGoodsDto> goodsMap = freGoodsService.findGoodsInfos(ids);
        return Result.newInstance().setMessage("查询成功").setData(goodsMap);
    }

    /**
    * @作者: 倪西
    * @描述：查询单个商品的详细信息
    * @时间：2022/2/26 10:05
    * @参数：[id]
    * @返回：com.hnzq.cloud.model.Result<com.hnzq.cloud.dto.FreGoodsDto>
    **/
    public Result<FreGoodsDto> findFreGoodsInfo(Integer id){
        FreGoodsDto goodsInfo = freGoodsService.findFreGoodsInfo(id);
        return Result.newInstance().setMessage("查询成功").setData(goodsInfo);
    }



}

